package ams.action;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;

import com.opensymphony.xwork2.ModelDriven;

import ams.model.dto.DataGrid;
import ams.model.entity.Rights;
import ams.model.entity.Role;
import ams.service.IRoleService;
import ams.util.SysUtil;

@Namespace("/role")
public class RoleAction extends BaseAction<Role>implements ModelDriven<Role> {

	private static final long serialVersionUID = 1L;

	@Autowired
	private IRoleService roleService;

	/**
	 * 接收的参数
	 */
	private Role role = new Role();

	private List<Role> roleList = new ArrayList<>();
	private List<Rights> rightsList = new ArrayList<>();
	
	@Action(value="roleAdmin",results={@Result(location="/WEB-INF/jsp/role.jsp")})
	public String roleAdmin(){
		return SUCCESS;
	}

	// 添加角色
	@Action(value = "add", results = { 
			@Result(type = "json", params = { "root", "execResult", "excludeNullProperties","true", "contentType", "text/html" }) })
	public String add() {
		super.setExecResult(roleService.add(role));
		return SUCCESS;
	}

	// 删除角色
	@Action(value = "delete", results = {
			@Result(type = "json", params = { "root", "execResult", "excludeNullProperties", "true" }) })
	public String delete() {
		String[] idStrArray = StringUtils.split(super.ids, ",");
		super.setExecResult(roleService.delete(SysUtil.strArray2IntList(idStrArray)));
		return SUCCESS;
	}

	// 修改角色
	@Action(value = "update", results = { 
			@Result(type = "json", params = { "root", "execResult", "excludeNullProperties", "true", "contentType", "text/html" }) })
	public String update() {
		super.setExecResult(roleService.update(role));
		return SUCCESS;
	}

	// 角色dataGrid
	@Action(value = "dataGrid", results = {
			@Result(type = "json", params = { "root", "dataGrid", "excludeNullProperties", "true" ,"excludeProperties","dataGrid.rows"}) })
	public String dataGrid() {
		List<Role> roleList = roleService.rolePage(page, rows);
		Long total = roleService.totalCount();
		super.setDataGrid(new DataGrid<Role>(total, roleList));
		return SUCCESS;
	}

	// 获取角色中授权的角色
	@Action(value = "getRoleRights", results = {
			@Result(type = "json", params = { "root", "rightsList", "excludeNullProperties", "true" }) })
	public String getRoleRights() {
		rightsList = roleService.getRoleRights(role);
		return SUCCESS;
	}
	// 获取角色中授权的角色
	@Action(value = "updateRoleRights", results = {
			@Result(type = "json", params = { "root", "execResult", "excludeNullProperties", "true" }) })
	public String updateRoleRights() {
		String[] idStrArray = StringUtils.split(ids, ","); 
		super.setExecResult(roleService.updateRoleRights(role.getId(), SysUtil.strArray2IntList(idStrArray)));
		return SUCCESS;
	}
	// 获取角色目录
	@Action(value = "roleCatalog", results = {
			@Result(type = "json", params = { "root", "roleList", "excludeNullProperties", "true" }) })
	public String roleCatalog() {
		this.roleList = roleService.roleCatalog();
		return SUCCESS;
	}
	
	

	@Override
	public Role getModel() {
		return role;
	}

	public Role getRole() {
		return role;
	}

	public void setRole(Role role) {
		this.role = role;
	}

	public List<Role> getRoleList() {
		return roleList;
	}

	public void setRoleList(List<Role> roleList) {
		this.roleList = roleList;
	}

	public List<Rights> getRightsList() {
		return rightsList;
	}

	public void setRightsList(List<Rights> rightsList) {
		this.rightsList = rightsList;
	}

}
